package com.jiankangjin.transsystem.controller;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiankangjin.transsystem.services.CreateOrderServices;
import com.jiankangjin.transsystem.util.StringUtil;
import com.jiankangjin.transsystem.vo.CreateVo;

/**
 * 健康金交易系统服务接口
 * v0.0.2 neason
 * v0.3 liuzy
 */
@Controller
public class CreateOrderController {
    private static final Log log = LogFactory.getLog(CreateOrderController.class);

    @Autowired
    private CreateOrderServices createOrderServices;
    
    /**
     * 订单创建
     */
    @RequestMapping(value = "/create_order", method = RequestMethod.POST)
    @ResponseBody
    public CreateVo createOrder(
            @RequestParam(required = false) String module_id,
            @RequestParam(required = false) String version,
            @RequestParam(required = false) String sign,
            @RequestParam(required = false) String merchant_order_id,
            @RequestParam(required = false) String merchant_id,
            @RequestParam(required = false) String store_name,
            @RequestParam(required = false) Integer total_fee,
            @RequestParam(required = false) String currency,
            @RequestParam(required = false) String content,
            @RequestParam(required = false) String busi_type,
            @RequestParam(required = false) String goods_count,
            @RequestParam(required = false) String goods_detail,
            @RequestParam(required = false) String diag_detail,
            @RequestParam(required = false, defaultValue = "true") Boolean is_tip,
            @RequestParam(required = false) String buyer_name,
            @RequestParam(required = false) String buyer_cellphone,
            @RequestParam(required = false) String page_callback,
            @RequestParam(required = false) String order_callback,
            @RequestParam(required = false) String comment,
            
            @RequestParam(required = false) String token,
            @RequestParam(required = false) String operator_id,
            @RequestParam(required = false) String operator_name,
            @RequestParam(required = false) String order_total_fee,//订单总额-折扣金额=订单金额(订单表中的ordAmt)
            @RequestParam(required = false) String discount_fee,
            @RequestParam(required = false) String discount_comment,
            
			@RequestParam(required = false) String local_pay_ssn,//如果本地支付流水不为空，不但创建订单而且支付
			@RequestParam(required = false) String fee,
			@RequestParam(required = false) String channel_id,
			@RequestParam(required = false) String buyer_token,
			@RequestParam(required = false) String buyer_voucher,
			@RequestParam(required = false) String channel_type,
			
            @RequestParam(required = false) String detail,
            @RequestParam(required = false) String ext1,
            @RequestParam(required = false) String ext2
            ) {
		if (log.isInfoEnabled()) {
			StringBuilder sb = new StringBuilder();
			sb.append("module_id=").append(module_id);
			sb.append(" version=").append(version);
			sb.append(" sign=").append(sign);
			sb.append(" merchant_order_id=").append(merchant_order_id);
			sb.append(" merchant_id=").append(merchant_id);
			sb.append(" store_name=").append(store_name);
			sb.append(" total_fee=").append(total_fee);
			sb.append(" currency=").append(currency);
			sb.append(" content=").append(content);
			sb.append(" goods_count=").append(goods_count);
			sb.append(" goods_detail=").append(goods_detail);
			sb.append(" diag_detail=").append(diag_detail);
			sb.append(" is_tip=").append(is_tip);
			sb.append(" buyer_name=").append(buyer_name);
			sb.append(" buyer_cellphone=").append(buyer_cellphone);
			sb.append(" page_callback=").append(page_callback);
			sb.append(" order_callback=").append(order_callback);
			sb.append(" comment=").append(comment);
			sb.append(" busi_type=").append(busi_type);
			sb.append(" token=").append(token);
			sb.append(" operator_id=").append(operator_id);
			sb.append(" operator_name=").append(operator_name);
			sb.append(" detail=").append(detail);
			sb.append(" ext1=").append(ext1);
			sb.append(" ext2=").append(ext2);
			log.info("调用创建订单接口 " + sb);
		}
        CreateVo createVo = new CreateVo();
        createVo.setResp_status("9999");
        if (StringUtil.isEmpty(merchant_id)) {
        	createVo.setMsg("商户编号为必填项不能为空");
        	return createVo;
        }
        if (StringUtil.isEmpty(total_fee.toString())) {
        	createVo.setMsg("订单总金额 为必填项不能为空");
        	return createVo;
        }
        if (StringUtil.isEmpty(order_callback)) {
        	createVo.setMsg("订单处理通知地址跳转地址额 为必填项不能为空");
        	return createVo;
        }
        
        if (StringUtil.isEmpty(merchant_id)) {
        	createVo.setMsg("商户编号为必填项不能为空");
        	return createVo;
        }
        
        if (StringUtil.isEmpty(module_id)) {
        	createVo.setMsg("请求模块编号 为必填项不能为空");
        	return createVo;
        }
        Integer _fee = 1;
        if (StringUtil.isNotEmpty(local_pay_ssn)) {
        	// 如果本地支付流水不为空，不但创建订单而且支付
        	if (StringUtil.isEmpty(fee)) {
        		createVo.setMsg("fee不能为空");
        		return createVo;
        	} else {
        		if (StringUtil.isNotNum(fee)) {
        			createVo.setMsg("fee必须是数字");
        			return createVo;
        		} else {
        			_fee = Integer.parseInt(fee);
        			if (_fee <= 0) {
        				createVo.setMsg("fee必须大于0");
        				return createVo;
        			}
        		}
        	}
        	if (StringUtil.isEmpty(channel_id)) {
        		createVo.setMsg("channel_id不能为空");
        		return createVo;
        	}
        	if (StringUtil.isEmpty(buyer_token)) {
        		createVo.setMsg("buyer_token不能为空");
        		return createVo;
        	}
        	if (StringUtil.isEmpty(buyer_voucher)) {
        		createVo.setMsg("buyer_voucher不能为空");
        		return createVo;
        	}
        	if (StringUtil.isEmpty(channel_type)) {
        		createVo.setMsg("channel_type不能为空");
        		return createVo;
        	}
        }
		if (StringUtil.isNotEmpty(local_pay_ssn)) {
			// 创建并支付
			return createOrderServices.createOrderAndPay(module_id, version,
					sign, merchant_order_id, merchant_id, store_name,
					total_fee, currency, content, goods_count, goods_detail,
					diag_detail, is_tip, buyer_name, buyer_cellphone,
					page_callback, order_callback, comment, busi_type, token,
					operator_id, operator_name, order_total_fee, discount_fee,
					discount_comment, detail, ext1, ext2, local_pay_ssn, fee,
					channel_id, buyer_token, buyer_voucher, channel_type);
		} else if ("v0.3".equals(version) || "v0.3.0".equals(version)) {
			// 0.3.x版
			return createOrderServices.createOrderV030(module_id, version,
					sign, merchant_order_id, merchant_id, store_name,
					total_fee, currency, content, goods_count, goods_detail,
					diag_detail, is_tip, buyer_name, buyer_cellphone,
					page_callback, order_callback, comment, busi_type, token,
					operator_id, operator_name, order_total_fee, discount_fee,
					discount_comment, detail, ext1, ext2);
		} else {
			// 0.0.2版
			return createOrderServices.createOrderV002(module_id, version,
					sign, merchant_order_id, merchant_id, store_name,
					total_fee, currency, content, goods_count, goods_detail,
					diag_detail, is_tip, buyer_name, buyer_cellphone,
					page_callback, order_callback, comment, busi_type, token,
					operator_id, operator_name, order_total_fee, discount_fee,
					discount_comment, detail, ext1, ext2);
		}
    }
}
